<template>
  <div class="content">
    <div class="common-seach-wrap" :style="{ marginBottom: '10px' }">
      <el-button
        type="primary"
        icon="Plus"
        @click="handleOpen"
        v-auth="'examine_add'"
        >添加考核</el-button
      >
    </div>
    <el-row align="top" :gutter="20">
      <el-col :span="3">
        <el-card header="选择部门">
          <el-tree
            :data="departmentOption"
            node-key="id"
            default-expand-all
            check-on-click-node
            show-checkbox
            :expand-on-click-node="false"
            check-strictly
            :props="{ label: 'title', value: 'id' }"
            ref="treeRef"
            :highlight-current="true"
            @check-change="handleStaff"
          ></el-tree>
        </el-card>
      </el-col>
      <el-col :span="21">
        <el-card>
          <el-table :data="tableData" row-key="id" height="100%" stripe>
            <el-table-column type="index" label="#" fixed="left" />
            <el-table-column prop="title" label="标题" fixed="left" />
            <el-table-column
              prop="department.title"
              sortable
              label="所属部门"
            />
            <el-table-column prop="create_time" sortable label="创作时间" />
            <el-table-column prop="update_time" sortable label="最后修改" />
            <el-table-column prop="sort" sortable label="排序" />
            <el-table-column label="操作" fixed="right" width="200">
              <template #default="scope">
                <el-button
                  type="primary"
                  link
                  icon="Edit"
                  @click="handleOpen(scope.row.id)"
                  v-auth="'examine_edit'"
                >
                  编辑
                </el-button>
                <el-button
                  type="danger"
                  link
                  icon="DeleteFilled"
                  @click="delInfo(scope.row.id)"
                  v-auth="'examine_del'"
                >
                  删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-divider border-style="none" />
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            background
            :current-page="curPage"
            :page-size="pageSize"
            layout="total, sizes,prev, pager, next, jumper"
            :total="total"
          ></el-pagination>
        </el-card>
      </el-col>
    </el-row>
    <Drawer
      ref="examineDraw"
      @getTable="getTableInfo"
      :department="departmentOption"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, defineEmits, reactive, watch } from "vue";
import {
  deleteDepartment,
  getDepartmentOptionsApi,
} from "@/api/company/department";
import Drawer from "./component/dialogForm.vue";
import { ElMessage, ElMessageBox, TabPaneName } from "element-plus";
import { getListAPi } from "@/api/company/examine";
const activeName = ref("0");
const tabs = ref<any[]>([]);
const classVisible = ref(false);
const classDialogTitle = ref("添加分类");
const examineDraw = ref<any>(null);
const classForm = ref({
  id: 0,
  title: "",
  sort: 0,
});
const query = reactive({
  department_id: [],
  class_id: "0",
});
const total = ref(0);
watch(
  query,
  () => {
    getTableInfo();
  },
  { deep: true }
);
watch(activeName, (newVal) => {
  query.class_id = newVal;
});
const curPage = ref(1);
const pageSize = ref(20);
const departmentOption = ref<any[]>([]);
const childDialogForm = ref<any>(null);
const tableData = ref([]);
const staffRef = ref<any>(null);
onMounted(() => {
  getDepartmentOptionsApi().then((res) => {
    departmentOption.value = res.data;
  });
  getTableInfo();
});
const handleSizeChange = (val: number) => {
  pageSize.value = val;
  getTableInfo();
};
const handleCurrentChange = (val: number) => {
  curPage.value = val;
  getTableInfo();
};
function getTableInfo() {
  getListAPi({
    page: curPage.value,
    page_size: pageSize.value,
    ...query,
  }).then((res) => {
    tableData.value = res.data.data;
    total.value = res.data.total;
  });
}

function delInfo(id: number) {
  ElMessageBox.confirm("确认要删除此信息吗?", "警告！", {
    confirmButtonText: "确认删除",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      deleteDepartment(id).then((res: any) => {
        getTableInfo();

        ElMessage({
          type: "success",
          message: "删除成功！",
        });
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "删除已取消",
      });
    });
}

// 打开添加数据窗口
const emits = defineEmits(["openForm", "search"]);
function handleOpen(id?: number) {
  examineDraw.value.dialogOpen(id || 0);
}

const handleStaff = (e: any, checked: boolean) => {
  if (checked) {
    query.department_id.push(e.id);
  } else {
    query.department_id = query.department_id.filter(
      (item: any) => item != e.id
    );
  }
};

defineExpose({
  getTableInfo,
});
</script>

<style lang="scss">
.el-tabs {
  .el-tabs__new-tab {
    width: auto !important;
  }
}
.table-box {
  height: calc(100% - 200px);
}
</style>
